package com.yyl.systemdemo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyl.entity.ERole;
import com.yyl.systemdemo.service.IERoleService;
import com.yyl.util.PageInfo;
import com.yyl.util.Result;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuyongli
 * @since 2022-12-28
 */
@RestController
@RequestMapping("/e-role")
@Api(tags="角色信息api")
public class ERoleController {

    @Resource
    private IERoleService roleService;

    // 查询所有的role
   @PostMapping
   @ApiOperation("查询角色的信息")
    public Result getAllRole(@ModelAttribute PageInfo page,@ModelAttribute ERole role){
        Page page1 = roleService.getPageData(page, role);
        return new Result(page1);
    }

    /**
     * 添加角色的信息
     *
     */
    @PostMapping("addRole")
    @ApiOperation("添加角色的信息")
    public Result addRole(@ModelAttribute ERole role){
        // saveOrUpdate  添加或者是修改
        // role 对象有id的值的时候 修改
        // id 没有值的时候执行的是添加
        return new Result(roleService.saveOrUpdate(role));
    }


    @PostMapping("upStatus/{id}/{status}")
    @ApiOperation("修改角色的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" ,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "status",dataType = "String",paramType = "query")
    })
    public Result updRole(@PathVariable @RequestParam Integer id,@PathVariable @RequestParam String status){

        ERole role = new ERole();
        role.setStatus(status);
        role.setId(id);
        //boolean update = roleService.update(updateWrapper);
        return new Result(roleService.updateById(role));
    }

    /**
     * 删除角色的信息
     */
    @DeleteMapping("{id}")
    @ApiOperation("根据id删除角色的信息")
    public Result delRole(@PathVariable @ApiParam("角色的id") Integer id){
        return new Result(roleService.removeById(id));
    }
    /**
     * 权限分配的时候 使用的数据
     */
    @ApiOperation("根据角色的id修改角色的权限")
    @PostMapping("selectPermission/{id}")
    public Result selPermission(@PathVariable @ApiParam("角色的id") Integer id){
        return new Result(roleService.getPermissionData(id));
    }
}

